<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="数据源名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sourceName">
              <a-input v-model="model.sourceName" placeholder="请输入数据源名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="数据源类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sourceType">
              <a-select style="width: 200px" placeholder="请选择数据源类型" v-model="model.sourceType">
                <a-select-option
                :key="item.tid"
                v-for="item in options"
                :value="item.tid"
                >{{item.sourceName}}</a-select-option>
            </a-select>
            </a-form-model-item>

          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction,postAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'CollectSourceForm',
  components: {
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {
      },
      options:[],
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        tid: [
          { required: true, message: '请输入主键!' },
        ],
      },
      url: {
        add: "/dcloud-platform/api/collectSource/v1/add",
        edit: "/dcloud-platform/api/collectSource/v1/edit",
        queryById: "/dcloud-platform/api/collectSource/v1/queryById",
        getAllSourceType: "/dcloud-platform/api/collectSourceDefine/v1/getAllList"
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
    this.getAllSourceType()
  },
  methods: {
    add() {
      this.edit({});
    },
    edit(record) {
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    getAllSourceType(){
      postAction(this.url.getAllSourceType, {}).then(res => {
        if (res.success) {
              console.log(res)
              this.options = res.result
              // if(res.result.length > 0){
              //   this.model.sourceType = res.result[0].tid
              // }
            } else {
              this.$message.warning(res.message)
            }
      })
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.tid) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
  }
}
</script>